/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#ifndef INCLUDE_SHADER_COLOR
#define INCLUDE_SHADER_COLOR

#include "shader_built_in_functions.h"
#include "shader_binding_ID.h"




#define DEF_DrawType_LS						0
#define DEF_DrawType_Weigth					1

//图标着色类型
#define DEF_DrawType_ICO_ColorInvert		2
//相乘输入颜色图标
#define DEF_DrawType_ICO_Color				3
//已图标透明通道混合输入颜色 圆形背景图标
#define DEF_DrawType_ICO_CircularBackground	4
//圆形透明通道
#define DEF_DrawType_Col_CircularBackground	5

#define DEF_DrawType_Normal_To_Col			6			//法线转颜色

//单色图标
#define DEF_ICO_Color_Monochrome			7

//线性绘制
#define DEF_DrawType_Linear			        8

//线性绘制
#define DEF_DrawType_Depth			        9

//动态单色
#define DEF_ICO_DynamicMonochrome           10





/******************************** CUBE 纹理方向 ********************************/
#define DEF_ObType_CubeTexDir_PX      10
#define DEF_ObType_CubeTexDir_NX      11
#define DEF_ObType_CubeTexDir_PY      12
#define DEF_ObType_CubeTexDir_NY      13
#define DEF_ObType_CubeTexDir_PZ      14
#define DEF_ObType_CubeTexDir_NZ      15





#ifdef __cplusplus

#include "basic_def.h"
#include "basic_vec.h"

// GLSL Type
using uint = unsigned int;

#define in
#define out
#define inout



static vec2 cw_V0 = vec2{ 0.0f, 1 };
static vec2 cw_V1 = vec2{ 0.86603f, -0.5f };
static vec2 cw_V2 = vec2{ -0.86603f, -0.5f };

/*Inline vec3 mix(const vec3& v1, const vec3& v2, float32 a) {
	vec3 outVal;
	outVal.x = v1.x * (1 - a) + v2.x * a;
	outVal.y = v1.y * (1 - a) + v2.y * a;
	outVal.z = v1.z * (1 - a) + v2.z * a;
	return outVal;
}*/


#else

vec2 cw_V0 = vec2(0.0, 1);
vec2 cw_V1 = vec2(0.86603, -0.5);
vec2 cw_V2 = vec2(-0.86603, -0.5);

#define Inline



vec3 f_glsl_WeigthToColor(float weigth) {
	vec3 Color;
	if (weigth < 0.0f) {
		Color.rgb = vec3(0, 0, 0.5);
	}
	else if (weigth < 0.2f) {
		//Color.r = 0.5 * (1.0-vWeigth/0.2);
		Color.r = 0.0;
		Color.g = 0.0;
		Color.b = 0.5 + (0.5 * weigth / 0.2);
	}
	else if (weigth >= 0.2 && weigth < 0.4) {
		Color.r = 0.0;
		Color.g = 5.0 * (weigth - 0.2);
		Color.b = 1.0;
	}
	else if (weigth >= 0.4 && weigth < 0.6) {
		Color.r = 0.0;
		Color.g = 1.0;
		Color.b = 5.0 * (0.6 - weigth);
	}
	else if (weigth >= 0.6 && weigth < 0.8) {
		Color.r = 5.0 * (weigth - 0.6);
		Color.g = 1.0;
		Color.b = 0.0;
	}
	else if (weigth >= 0.8 && weigth <= 1) {
		Color.r = 1.0;
		Color.g = 5.0 * (1.0 - weigth);
		Color.b = 0.0;
	}
	else {
		//Color.rgb = vec3(1,1,1);
		Color.rgb = vec3(1, 1, 1);
	}
	return Color;
}


#endif



#include "texture.h"





struct S_ColorTake {
	vec3 color;
	vec2 coord;
	//����
	float intensity;
};

struct S_ColorGradient {
	float take_r;
	float take_g;
	float take_b;
	float take_a;
	uint controlPointNum;
	uint dir;
};

struct S_LineProp {
	float	m_Gap;
	uint	m_ColorA;
	uint	m_ColorB;
};


Inline vec4 f_glsl_buildColor(in uint color) {
	vec4 TColor;
	TColor.x = float((color & 0xff000000) >> 24) / 255.0;
	TColor.y = float((color & 0x00ff0000) >> 16) / 255.0;
	TColor.z = float((color & 0x0000ff00) >> 8) / 255.0;
	TColor.w = float((color & 0x000000ff)) / 255.0;
	return TColor;
};

Inline vec3 f_glsl_RGB_to_HSV(in vec3 rgb) {
	vec3 HSV;
	float ma = max(max(rgb.x, rgb.y), rgb.z);
	float mi = min(min(rgb.x, rgb.y), rgb.z) + 0.0000001;

	HSV.z = ma;
	HSV.y = (ma - mi) / ma;

	if (rgb.x == ma) HSV.x = (rgb.y - rgb.z) / (ma - mi) * 60.0f;
	if (rgb.y == ma) HSV.x = (rgb.z - rgb.x) / (ma - mi) * 60.0f + 120;
	if (rgb.z == ma) HSV.x = (rgb.x - rgb.y) / (ma - mi) * 60.0f + 240;

	if(HSV.x < 0) HSV.x += 360.0f;

	return HSV;
}



#undef in
#undef out

#endif